local scoreboard = nil;

function GM:ScoreboardShow()
	if (!scoreboard or !scoreboard:IsValid()) then
		scoreboard = vgui.Create("lgtrp_scoreboard");
	else
		scoreboard:SetVisible(true);
		scoreboard:Update();
	end;
	
	gui.EnableScreenClicker(true);
end;

function GM:ScoreboardHide()
	scoreboard:SetVisible(false);
	gui.EnableScreenClicker(false);
end;

local PANEL = {};

function PANEL:Init()
	self.count = 0;
	
	self:SetSize(400, 350);
	self:SetPos(ScrW() /2 -200, ScrH() /2 -175);
	
	self.list = vgui.Create("DPanelList", self);
	self.list:SetPos(4, 50);
	self.list:SetSize(self:GetWide() -8, self:GetTall() -54);
	self.list:SetSpacing(2);
	self.list:SetPadding(2);
	self.list:EnableVerticalScrollbar(true);
	self.list.Paint = function(self)
		local w, h = self:GetSize();
		
		draw.RoundedBox(4, 0, 0, w, h, Color(0, 0, 0, 200));
		draw.DrawSimpleRect(1, 1, w -2, h -2, Color(81, 81, 81, 200));
	end;
end;

function PANEL:Update()
	self.list:Clear();

	local teams = team.GetAllTeams();

	for k, v in pairs(teams) do
		if (k != 1001 and k != 1002 and k != 0) then
			local hasPlayer = team.GetPlayers(k);
			
			if (#hasPlayer > 0) then
				local category = vgui.Create("DForm");
				category:SetAutoSize(true);
				category:SetName(v.Name);
				category:SetPadding(2);
				category:SetSpacing(2);
				category.Label:SetColor(team.GetColor(k));
				category.Paint = function() end;
				
				timer.Simple(0.01, function()
					category.Label:SetColor(team.GetColor(k));
				end);
				
				for k2, v2 in pairs(player.GetAll()) do
					if (v2:Team() == k) then
						local panel = vgui.Create("DPanel");
						panel:SetTall(32);
						panel.Paint = function(self)
							local w, h = self:GetSize();
							
							draw.DrawSimpleRect(0, 0, w, h, Color(81, 81, 81, 210));
							
							draw.SimpleText(v2:Nick(), "TargetID", 36, 9, color_black, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER);
							draw.SimpleText(v2:Nick(), "TargetID", 35, 8, color_white, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER);
							
							draw.SimpleText(v2:Title(), "TargetID", 36, 24, Color(0, 0, 0, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER);
							draw.SimpleText(v2:Title(), "TargetID", 35, 23, Color(140, 140, 140, 255), TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER);
							
							draw.SimpleText(v2:Ping(), "TargetID", w -9, h /2, color_black, TEXT_ALIGN_RIGHT, TEXT_ALIGN_CENTER);
							draw.SimpleText(v2:Ping(), "TargetID", w -10, h /2 -1, Color(180, 180, 180, 255), TEXT_ALIGN_RIGHT, TEXT_ALIGN_CENTER);
						end;
						
						local icon = vgui.Create("SpawnIcon", panel);
						icon:SetIconSize(32);
						icon:SetModel(v2:GetModel());
						icon:SetToolTip(nil);
						
						category:AddItem(panel);
					end;
				end;
				
				self.list:AddItem(category);
			end;
		end;
	end;
end;

function PANEL:Think()
	if (self:IsVisible() and self.count != #player.GetAll()) then
		self:Update();
		
		self.count = #player.GetAll();
	end;
end;

function PANEL:Paint()
	local w, h = self:GetSize();
	
	draw.RoundedBox(4, 0, 0, w, h, Color(0, 0, 0, 200));
	draw.DrawSimpleRect(1, 1, w -2, h -2, Color(81, 81, 81, 200));
	
	draw.SimpleText("LightRP 2", "lgtrp_scoreboardHeader", 20, 30, color_white, TEXT_ALIGN_LEFT, TEXT_ALIGN_CENTER);
end;

vgui.Register("lgtrp_scoreboard", PANEL, "DPanel");